package org.gwtEss.portlet;

import java.util.Vector;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Widget;

public class PortletCol extends Vector<Widget>{//We are extending Vector instead of Arraylist because we need to insert objects in between. 
	private AbsolutePanel apRef;
	private int refRelLeft;
	private int width;
	private int vPadding = 10; 
	public PortletCol(AbsolutePanel apRef, int refRelLeft, int width) {
		this.apRef = apRef; 
		this.refRelLeft = refRelLeft; 
		this.width = width;
	}
	int getCurrMaxHeight(){
		int size = size();
		if (size == 0) return 0;
		Widget p = get(size() - 1); 
		return apRef.getWidgetTop(p) + p.getOffsetHeight();
	}
	public void addPortlet(final Widget newPortletInstance) {
		apRef.add(newPortletInstance);

		DeferredCommand.addCommand(new Command(){

			public void execute() {
				apRef.setWidgetPosition(newPortletInstance, refRelLeft, getCurrMaxHeight() + vPadding);
				add(newPortletInstance);
			}
			
		});
	}
	/*public void removePortlet(Widget portlet) {
		System.out.println("add: " +  ((newElement instanceof DropHereWidget)?"DropHereWidget": "PlaceHolder"));
		super.remove(portlet);
		renderAll();
	}*/
	public void renderAll() {
		Widget lastP = null; 
		int size = super.size(); 
		for (int i=0; i<size; i++){
			Widget p = super.get(i);
			int top = lastP == null?0:(apRef.getWidgetTop(lastP) + lastP.getOffsetHeight());
			apRef.setWidgetPosition(p, refRelLeft, top + vPadding);
			
			lastP = p;
		}
	}
/*	public void replacePortlet(Widget existingWidget, Widget replacementWidget) {
		int index = super.indexOf(existingWidget);
		if (index == -1) return;
		super.set(index, replacementWidget);
		renderAll();
	}*/
	/*public int findBelongingCell(int top) {
		int topSoFar = 0;
		int size = super.size();
		for (int i =0; i<size; i++ ){
			int wHeight = super.get(i).getOffsetHeight();
			if (top >= topSoFar && top<topSoFar + wHeight + vPadding)
				return i; 
			topSoFar += wHeight + vPadding;
		}
		return size-1;
	}*/
	
	double ALLOWER_RANGE_PERCENT = 0.25;
/*	public int findBelongingCell(Portlet source){
		int top = source.getPopupTop() + source.getOffsetHeight()/2;
		int size = super.size();
		int wHeight;
		int heightUpper = -1, heightLower = -1, wTop = -1;
		System.out.println("---");
		for (int i =0; i<size; i++ ){
			wHeight = super.get(i).getOffsetHeight();
			heightUpper = (int) (wHeight - wHeight*(1-ALLOWER_RANGE_PERCENT));
			heightLower = (int) (wHeight - wHeight*ALLOWER_RANGE_PERCENT);
			wTop = super.get(i).getAbsoluteTop();
			System.out.println("top: " + top + ", heightUpper: " + (wTop + heightUpper) + ", heightLower: " + (wTop + heightLower));

			if (top > wTop + heightUpper && top < wTop + heightLower)
				return i; 
		}
		
		if (top > heightLower + wTop) return size;//TODO: consider the scenario where size = 0; 	
		return -1; 
	}*/
	
	public int findBelongingCell(Portlet source){
		int sourceHeight = source.getOffsetHeight();
		int sourceTop = source.getPopupTop();
		
		int sourceBottom = sourceTop + sourceHeight;
		int size = super.size();
		int wHeight = 0;
//		int wHeightUpper = -1, wHeightLower = -1, wTop = -1;
		System.out.println("---");
		int wTop = 0;
		for (int i =0; i<size; i++ ){
			wHeight = super.get(i).getOffsetHeight();
			double wHeightDelta = wHeight*ALLOWER_RANGE_PERCENT;
			double sourceHeightDelta = sourceHeight*ALLOWER_RANGE_PERCENT;
//			wHeightUpper = (int) (wHeight - wHeight*(1-ALLOWER_RANGE_PERCENT));
//			wHeightLower = (int) (wHeight - wHeight*ALLOWER_RANGE_PERCENT);
			wTop = super.get(i).getAbsoluteTop();
//			System.out.println("top: " + sourceTop + ", heightUpper: " + (wTop + wHeightUpper) + ", heightLower: " + (wTop + wHeightLower));

			
			System.out.println(
					", sourceTop: " + sourceTop + 
					", sourceBottom: " + sourceBottom + 
					", wTop: " + wTop + 
					", sourceTop: " + wHeightDelta 
					);
			if (
//					sourceTop < wTop + wHeight - wHeightDelta && 
//					sourceTop > wTop + wHeightDelta &&
//					
					sourceTop < wTop + sourceHeight - sourceHeightDelta && //wTop + wHeight - wHeightDelta && 
					sourceTop > wTop + sourceHeightDelta 
					) 
				return i; 
			

			if (
//					sourceBottom < wTop + wHeight - wHeightDelta && 
//					sourceBottom > wTop + wHeightDelta &&
//					
					sourceBottom < wTop + sourceHeight - sourceHeightDelta && 
					sourceBottom > wTop + sourceHeightDelta
					
			)
				return i; 
			
		}
		
		if (sourceTop > wTop + wHeight ) return size;//TODO: consider the scenario where size = 0; 	
		return -1; 
	}
	
	public int findBelongingCell11(int top) {
		int topSoFar = 0;
		int size = super.size();
		int wHeight = super.get(0).getOffsetHeight()/2  + vPadding;
		if (top < wHeight)
			return 0;
		topSoFar += wHeight;
		for (int i =1; i<size-1; i++ ){
			wHeight = super.get(i).getOffsetHeight();
//			if (top >= topSoFar && top<topSoFar + wHeight + vPadding)
			if (top < topSoFar + wHeight/2 + vPadding)
				return i; 
			topSoFar += wHeight/2 + vPadding ;
		}
		return size-1;
	}
	
	@Override
	public void add(int index, Widget element) {
		String str = (element instanceof DropHereWidget)?"DropHereWidget": "Portlet";
		System.out.println("add: " +  str + ", index = " + index + ", size: " + size());
		int size = size();
		super.add(index > size?size:index, element);
		renderAll();
	}
	@Override
	public boolean remove(Object o) {
		String str = (o instanceof DropHereWidget)?"DropHereWidget": "Portlet";
		System.out.println("remove: " +  str);
		boolean success = super.remove(o);
		if (success)
			renderAll();
		return success; 
	}
	/*public void replace(int index, Widget element) {
		super.remove(index);
		super.add(index, element);
		renderAll();
	}*/
	public void replace(Widget oldElement, Widget newElement) {
		System.out.println("remove: " +  ((oldElement instanceof DropHereWidget)?"DropHereWidget": "Portlet"));
		System.out.println("add: " +  ((newElement instanceof DropHereWidget)?"DropHereWidget": "Portlet"));
		int index = super.indexOf(oldElement);
		if (index == -1) assert true: "Invalid condition. Place holder not fould in current portlet container column";
		super.remove(index);
		int size = size();
		super.add(index > size?size:index, newElement);
		renderAll();
	}
	

}